package org.sam.swing.table.propertytable;

import java.text.ParseException;

import javax.swing.JSpinner;
import javax.swing.SpinnerModel;

import com.l2fprod.common.beans.editor.AbstractPropertyEditor;
import com.l2fprod.common.util.converter.ConverterRegistry;

/**
 * 微调属性设置控件
 * 
 * @author sam
 *
 */
public class SpnnierPropertyEditor extends AbstractPropertyEditor {

	/**
	 * 绑定的类型
	 */
	private final Class<?> type;

	/**
	 * 微调属性设置控件
	 * 
	 * @param type
	 */
	public SpnnierPropertyEditor(Class<?> type) {
		this.type = type;
		this.editor = new JSpinner();
	}

	/**
	 * 微调属性设置控件
	 * 
	 * @param type
	 * @param model
	 */
	public SpnnierPropertyEditor(Class<?> type, SpinnerModel model) {
		this.type = type;
		this.editor = new JSpinner(model);
	}

	/**
	 * 获取值
	 */
	public Object getValue() {
		try {
			((JSpinner) editor).commitEdit();
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return ((JSpinner) editor).getValue();

	}

	/**
	 * 设置值
	 */
	public void setValue(Object value) {
		if (value == null)
			((JSpinner) editor).setValue(getDefaultValue());
		else
			((JSpinner) editor).setValue(value);
	}

	/**
	 * 获取默认值
	 * 
	 * @return
	 */
	private Object getDefaultValue() {
		return ConverterRegistry.instance().convert(this.type, "0");
	}
}
